/**
 * 
 */
package xx.services.teach;

import javax.servlet.http.HttpServletRequest;
import javax.sql.RowSet;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import xx.services.Passport;
import xx.services.util.Constants;
import xx.teach.entity.Account;
import xx.teach.entity.Question;

/**
 * @author Administrator 2014-3-19
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/teach/account")
public class AccountRest extends Passport
{

    /**
     * @param request
     */
    public AccountRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    @GET
    @Path("/login")
    public String login(@QueryParam("name")
    String name)
    {
        {
            try
            {
                Account account = new Account(qr, owner);
                RowSet rs = account.setName(name).getAccountAndGroup();
                return ok(rs);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return error(e.getMessage());
            }
        }
    }

    /**
     * 修改account <br>
     * 只能修改以下字段：phone，password，name
     * 
     * @param groupId
     * @param content
     * @return
     */
    @PUT
    @Path("/{account_id}")
    public String updateMemo(@PathParam("account_id")
    long accountId, @FormParam("phone")
    String phone, @FormParam("password")
    String password, @FormParam("name")
    String name)
    {
        try
        {
            Account account = new Account(qr, owner);
            account.setAccountId(accountId);

            StringBuilder sb = new StringBuilder().append(",");

            if (phone != null && !phone.isEmpty())
            {
                account.setPhone(phone);
                sb.append(Account.PHONE).append(",");
            }

            if (password != null && !password.isEmpty())
            {
                account.setPassword(password);
                sb.append(Account.PASSWORD).append(",");
            }

            if (password != null && !password.isEmpty())
            {
                account.setName(name);
                sb.append(Account.NAME).append(",");
            }

            int count = 0;
            String field = sb.toString().replaceFirst(",", "");
            if (field.length() > 0)
            {
                field = field.substring(0, field.length() - 1);
                count = account.update();
            }
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 删除指定accountId的账户，多个accountId之间用逗号“，”或者下划线“_”间隔
     * 
     * @param accountIdArr
     * @return
     */
    @DELETE
    @Path("/{account_id}")
    public String deleteMemo(@PathParam("account_id")
    String accountIdArr)
    {
        try
        {
            String[] ids = accountIdArr.split("_|,");
            int count = 0;
            for (String accountId : ids)
            {
                Account account = new Account(qr, owner);
                account.setAccountId(Long.valueOf(accountId)).delete();
                count++;
            }

            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 新增question
     * 
     * @return
     */
    @POST
    @Path("/{account_id}/question")
    public String addMemo(@PathParam("account_id")
    long accountId, @FormParam("type")
    int type, @FormParam("content")
    String content, @FormParam("flag")
    int flag)
    {
        try
        {
            Question question = new Question(qr, owner);
            question.setAccountId(accountId);

            if (type == 0)
                type = Question.DOWN_CLASS_QUESTION;

            question.setType(type).setFlag(flag);

            if (content != null && !content.isEmpty())
                question.setContent(content);

            long questionId = question.insert();
            return ok(questionId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 设置未读信息为 已读信息
     * 
     * @param accountId
     * @return
     */
    @POST
    @Path("/{account_id}/question/read")
    public String updateReaded(@PathParam("account_id")
    long accountId)
    {

        try
        {
            Question question = new Question(qr, owner);
            int count = question.setAccountId(accountId).updateReaded();
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 根据accountId获取提问列表
     * 
     * @param accountId
     * @return
     */
    @GET
    @Path("/{account_id}/question")
    public String getQuestionByAccountId(@PathParam("account_id")
    long accountId)
    {
        try
        {
            Question question = new Question(qr, owner);
            question.setAccountId(accountId);
            RowSet rs = question.getQuestionByAccountId();
            return ok(rs);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 获取未回答的问题的列表（老师用）
     * 
     * @return
     */
    @GET
    @Path("/notreadaccount")
    public String getAccountByReaded()
    {
        try
        {
            Question question = new Question(qr, owner);
            RowSet rs = question.getAccountByReaded();
            return ok(rs);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }
}
